package com.suse.ecommerce.controller;

import com.suse.ecommerce.pojo.Product;
import com.suse.ecommerce.pojo.Shopping;
import com.suse.ecommerce.pojo.User;
import com.suse.ecommerce.service.ProductService;
import com.suse.ecommerce.service.ShoppingService;
import com.suse.ecommerce.uilts.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/auth")
public class ShoppingController {
    @Autowired
    private ShoppingService shoppingService;
    @Autowired
    private ProductService productService;
    //修改用户购物车的数量
    @RequestMapping("/updateShoppingNum")
    public Msg updateShoppingNum(@RequestParam("shoppingId")long shoppingId,@RequestParam("num")Integer num){
        shoppingService.updateShoppingNum(shoppingId,num);
        return Msg.success("更新成功");
    }
    //根据userId查询购物车信息
    @RequestMapping("/queryShopping")
    public Msg queryShopping(@SessionAttribute("user") User user){

        List<Shopping> shoppings = shoppingService.queryShopping(user.getUserId());
        if(shoppings.size()==0){
            return Msg.error("没有商品");
        }else{
            return Msg.success("查询成功").add("shoppings",shoppings);
        }
    }
    //添加商品到购物车
    @RequestMapping("/addShopping")
    public Msg addShopping(@SessionAttribute("user") User user,@RequestParam("productId")long productId,@RequestParam(value = "num",defaultValue = "1") int num){
        System.out.println(num);
        //根据productId出对应的商品
        Product product = productService.queryProductById(productId);
        if(product==null){
            return Msg.error("参数非法");
        }
        //根据userID和商品ID查询用户购物车中是否有数据
        long shoppingId = shoppingService.queryShoppingByPidAndUid(product.getId(), user.getUserId());
        //说明此时购物车没有该商品记录
        if(shoppingId==0){
            Shopping shopping=new Shopping(null,product.getId(),product.getTitle(),num,product.getPrice(),null,user.getUserId()
            ,new Date(),new Date(),product.getImage());
            shoppingService.addShopping(shopping);
        }
        else{
            //购物车中有该用户的商品记录，直接修改数据和总价即可
            shoppingService.updateShoppingNumAdd(shoppingId);
        }
        return Msg.success("加入成功");
    }

    @RequestMapping("/delShoppingById")
    public  Msg delShopping(Shopping shopping,@SessionAttribute("user") User user){
        if(shopping.getShoppingId() == null){
            return Msg.error("删除失败");
        }else {
            shopping.setUserId(user.getUserId());
            shoppingService.delShopping(shopping);
        }

        return Msg.success("删除成功");
    }
}
